package cn.ekuma.epos.datalogic.define.dao;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;

import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

import e.odbo.data.dao.BaseDAO;
import cn.ekuma.epos.symmetricds.AppConfig;
import cn.ekuma.epos.symmetricds.bean.NodeSecurity;

public class NodeSecurityDAO extends BaseDAO<NodeSecurity> {

	public NodeSecurityDAO(I_Session s) {
		super(s);
	}

	@Override
	public NodeSecurity readValues(DataRead dr,NodeSecurity obj) throws BasicException {
		if(obj==null)
			obj=new NodeSecurity();
		obj.setNodeId(dr.getString(1));
		obj.setNodePassword(dr.getString(2));
		obj.setRegistrationEnabled(dr.getBoolean(3));
		obj.setRegistrationTime(dr.getTimestamp(4));
		obj.setInitialLoadEnabled(dr.getBoolean(5));
		obj.setInitialLoadTime(dr.getTimestamp(6));
		obj.setCreatedAtNodeId(dr.getString(7));
		return obj;
	}

	@Override
	public void writeInsertValues(DataWrite dp, NodeSecurity obj)
			throws BasicException {
		dp.setString(1,obj.getNodeId());
		dp.setString(2,obj.getNodePassword());
		dp.setBoolean(3,obj.isRegistrationEnabled());
		dp.setTimestamp(4,obj.getRegistrationTime());
		dp.setBoolean(5,obj.isInitialLoadEnabled());
		dp.setTimestamp(6,obj.getInitialLoadTime());
		dp.setString(7,obj.getCreatedAtNodeId());
		
	}
	
	@Override
	public TableDefinition getTable() {
		return new TableDefinition(
				AppConfig.getAppProperty(AppConfig.TABLE_PREFIX)+"_"+"node_security",
                new Field[]{
			 new Field("node_id",Datas.STRING, Formats.STRING),
			 new Field("node_password",Datas.STRING, Formats.STRING),
			 new Field("registration_enabled",Datas.BOOLEAN, Formats.BOOLEAN),
			 new Field("registration_time",Datas.TIMESTAMP, Formats.TIMESTAMP),
			 new Field("initial_load_enabled",Datas.BOOLEAN, Formats.BOOLEAN),
			 new Field("initial_load_time",Datas.TIMESTAMP, Formats.TIMESTAMP),
			 new Field("created_at_node_id",Datas.STRING, Formats.STRING)
                 },
                new int[]{0}
                );
	}
	
	/**
	 *  <table name="node_security" description="Security features like node passwords and open registration flag are stored in the node_security table. ">
        <column name="node_id" type="VARCHAR" size="50" required="true" primaryKey="true"  description="Unique identifier for a node." />
        <column name="node_password" type="VARCHAR" size="50" required="true"  description="The password used by the node to prove its identity during synchronization." />
        <column name="registration_enabled" type="BOOLEANINT" size="1" default="0"  description="Indicates whether registration is open for this node.  Re-registration may be forced for a node if this is set back to '1' in a parent database for the node_id that should be re-registred." />
        <column name="registration_time" type="TIMESTAMP" required="false"  description="The timestamp when this node was last registered." />
        <column name="initial_load_enabled" type="BOOLEANINT" size="1" default="0"  description="Indicates whether an initial load will be sent to this node." />
        <column name="initial_load_time" type="TIMESTAMP" required="false"  description="The timestamp when this node started the initial load." />
        <column name="created_at_node_id" type="VARCHAR" size="50" required="true"  description="The node_id of the node where this node was created. This is typically filled automatically with the node_id found in node_identity where registration was opened for the node. " />
    </table>
	 */

    public  void reloadNode(String nodeId) throws BasicException {
    	
    }
	
	@Override
	public Class getSuportClass() {
		return NodeSecurity.class;
	}

}
